/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Converter;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

/**
 *
 * @author iron
 */
@FacesConverter("DoubleConverter")
public class DoubleConverter implements Converter{
/**
 *
 * @author iron
 */
    
    @Override
    public Object getAsObject(FacesContext fc, UIComponent uic, String string) {
        if(string.isEmpty())
            return null;
        Double valor = 0.0;
        string = string.replace(',', '.');
        try{
            valor = Double.parseDouble(string);
        }catch(Exception ex){
            ex.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL, "Entrada Vazia ou Incorreta.", null));
            return null;
        }
        return valor;
    }

    @Override
    public String getAsString(FacesContext fc, UIComponent uic, Object o) {
        return o.toString();
    }

}
